package com.speily.server.search.controller;

import com.speily.server.search.commons.BaseController;
import com.speily.server.search.service.ArticleService;
import com.speily.server.search.service.BaseSearchService;
import com.speily.server.search.service.ProductService;
import org.elasticsearch.client.transport.NoNodeAvailableException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @Auther: SPL
 * @Date: 2019-07-25 16:21
 * @Description: - 测试控制器
 */
@Controller
@RequestMapping("/search")
public class SearchController extends BaseController {

    @Autowired
    private ProductService productService ;
    @Autowired
    private ArticleService articleService ;

    @GetMapping("/orders/{keyword}")
    @ResponseBody
    public Object searchProduct(@PathVariable String keyword) {

        if(!StringUtils.isEmpty(keyword)){
            try{
                String indexName = "orders";
                List<Map<String,Object>> list = productService.queryHit(keyword,indexName,"productName","productDesc");
//                list.forEach(product -> System.out.println(product.toString()));
                return renderSuccess(list);
            }catch (NoNodeAvailableException e){
                e.printStackTrace();
                return renderError("ES节点不可用");
            }

        }else {
            return renderError("参数不能为空");
        }
    }

    @GetMapping("/blog/{keyword}")
    @ResponseBody
    public Object searchArticle(@PathVariable String keyword) {

        if(!StringUtils.isEmpty(keyword)){
            try{
                String indexName = "blog";
                List<Map<String,Object>> list = articleService.queryHit(keyword,indexName,"tittle","content");
                return renderSuccess(list);
            }catch (NoNodeAvailableException e){
                e.printStackTrace();
                return renderError("ES节点不可用");
            }

        }else {
            return renderError("参数不能为空");
        }
    }

}

